package com.zjeav.lingjiao.base.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zjeav.lingjiao.R;

/**
 * Created by 5zhi on 2016/9/12.
 */
public class TitleBar extends RelativeLayout {
    private Context mContext;
    private AttributeSet matts;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title_id)
    TextView title_txt;
    @BindView(R.id.im_right)
    ImageView im_right;
    @BindView(R.id.tv_save)
    TextView tv_save;
    @BindView(R.id.title_left)
    TextView tv_title_left;
    private int icon;

    public TitleBar(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        this.matts = attrs;
        init();
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        this.matts = attrs;
        init();
    }

    public void setTitle(String title) {
        title_txt.setText(title);
    }

    public void setLeftTile(String leftTile) {
        tv_title_left.setText(leftTile);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
        this.matts = attrs;
        init();
    }

    public void init() {
        LayoutInflater.from(mContext).inflate(R.layout.titlebar_layout, this);
        ButterKnife.bind(this);
        TypedArray typedArray = mContext.obtainStyledAttributes(matts, R.styleable.TitleBar);
        if (typedArray != null) {
            String title = typedArray.getString(R.styleable.TitleBar_title);
            String title_left = typedArray.getString(R.styleable.TitleBar_title_left);
            Boolean show = typedArray.getBoolean(R.styleable.TitleBar_back, true);
            Boolean home = typedArray.getBoolean(R.styleable.TitleBar_home, false);
            Boolean pic_show = typedArray.getBoolean(R.styleable.TitleBar_picshow, false);
            String rightname = typedArray.getString(R.styleable.TitleBar_rightname);
//            icon = typedArray.getResourceId(R.styleable.TitleBar_rightpic, R.mipmap.ic_add_human);
            if (home) {
//                back.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent(TitleBar.this.mContext, mContext.class);
//
//                        ((Activity) (TitleBar.this.mContext)).startActivity(intent);
//                        ((Activity) (TitleBar.this.mContext)).finish();
//                    }
//                });
            } else {
                back.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((Activity) (TitleBar.this.mContext)).finish();
                    }
                });
            }
            title_txt.setText(title);
            if (title_left != null && !"".equals(title_left)) {
                tv_title_left.setVisibility(VISIBLE);
                tv_title_left.setText(title_left);
            }
            if (show) {
                back.setVisibility(View.VISIBLE);
            } else {
                back.setVisibility(View.GONE);
            }
        }
    }


}
